package com.magicsoft.share.ui.promote;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.my.UserInfo;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.presenter.my.VipPresenter;
import com.magicsoft.share.ui.my.RecommendActivity;
import com.magicsoft.share.view.my.IVipView;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class PromoteFragment extends BaseFragment<IVipView, VipPresenter> implements IVipView {


    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.btn_recommend)
    Button btnRecommend;
    @BindView(R.id.btn_rule)
    Button btnRule;
    @BindView(R.id.btn_explain)
    Button btnExplain;
    @BindView(R.id.layout_vip)
    LinearLayout layoutVip;
    @BindView(R.id.btn_become_vip)
    Button btnBecomeVip;
    @BindView(R.id.btn_rule2)
    Button btnRule2;
    @BindView(R.id.btn_explain2)
    Button btnExplain2;
    @BindView(R.id.layout_vip_no)
    LinearLayout layoutVipNo;
    Unbinder unbinder;
    private AlertDialog edtDialog;
    public int payMethod;
    private AlertDialog payDialog;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_promote;
    }

    @Override
    public VipPresenter initPresenter() {
        return new VipPresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    protected void requestData() {

        mPresenter.userDetail();
    }


    @OnClick({R.id.btn_recommend, R.id.btn_rule, R.id.btn_explain, R.id.btn_become_vip, R.id.btn_rule2, R.id.btn_explain2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_recommend:
                startActivity(new Intent(getActivity(), RecommendActivity.class));
                break;
            case R.id.btn_rule:
                break;
            case R.id.btn_explain:
                break;
            case R.id.btn_become_vip:
                initEdtDialog();
                break;
            case R.id.btn_rule2:
                break;
            case R.id.btn_explain2:
                break;
        }
    }


    private void initEdtDialog() {
        View dialogEdtView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_edt_id, null);
        final EditText editText = (EditText) dialogEdtView.findViewById(R.id.edt);
        dialogEdtView.findViewById(R.id.btn_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String trim = editText.getText().toString().trim();
                if (edtDialog.isShowing()) {
                    edtDialog.dismiss();
                }
                initPayDialog();


            }
        });

        edtDialog = new AlertDialog.Builder(getContext()).setView(dialogEdtView)
                .show();
    }


    private void initPayDialog() {
        View dialogPayView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_pay_select, null);

        final RadioButton rbtnPayWeChat = (RadioButton) dialogPayView.findViewById(R.id.rbtn_pay_wechat);
        final RadioButton rbtnPayZFB = (RadioButton) dialogPayView.findViewById(R.id.rbtn_pay_zfb);
        rbtnPayWeChat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
                    rbtnPayZFB.setChecked(false);
                    payMethod = Constant.PAY_WEICHAT;

                }
            }
        });
        rbtnPayZFB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
                    rbtnPayWeChat.setChecked(false);
                    payMethod = Constant.PAY_ZFB;
                }
            }
        });


        dialogPayView.findViewById(R.id.btn_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (payDialog.isShowing()) {
                    payDialog.dismiss();
                }
                initFailDialog();


            }
        });

        payDialog = new AlertDialog.Builder(getContext()).setView(dialogPayView)
                .show();
    }

    private void initFailDialog() {
        new AlertView.Builder().setContext(getContext())
                .setStyle(AlertView.Style.Alert)
                .setTitle("支付失败")
                .setMessage(null)
                .setCancelText("确认")
                .setOthers(null)
                .build()
                .show();


    }

    @Override
    public void userDetailSuccess(UserInfo userInfo) {
        Log.d("lyf", "userDetailSuccess: "+userInfo.getMember()+"");
        if (userInfo.getMember() == 1) {
            layoutVip.setVisibility(View.VISIBLE);
            layoutVipNo.setVisibility(View.GONE);
            tvNum.setText(userInfo.getAccumulated_income() + "");
        } else {
            layoutVip.setVisibility(View.GONE);
            layoutVipNo.setVisibility(View.VISIBLE);
        }

    }
}
